package com.jty.module_user.viewmodel

import androidx.databinding.ObservableBoolean
import com.jty.lib_base.base.BaseBean
import com.jty.lib_base.base.BaseViewModel
import com.jty.lib_base.base.MyApplication
import com.jty.lib_base.data.DataRepository
import com.jty.lib_base.data.bean.TeacherPenBean
import com.jty.lib_base.extension.ApiSubscriptHelper
import com.jty.lib_base.utils.RxThreadHelper


/**
 * author: zhouyh
 * created on: 2021/9/18 5:35 下午
 * description:主页viewmodel
 */
class MyPenViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    var penMac:String = "MAC: " + (model.getPenMac()?:"")

    var penConnectState : ObservableBoolean = ObservableBoolean(false)

    override fun onCreate() {
        super.onCreate()
        tvTitle.set("我的智能笔")
        getTeacherPen()
    }

    private fun getTeacherPen(){

        model.apply {
            this.getTeacherPen()
                .compose(RxThreadHelper.rxSchedulerHelper(this@MyPenViewModel))
                .subscribe(object : ApiSubscriptHelper<BaseBean<TeacherPenBean>>() {
                    override fun onResult(t: BaseBean<TeacherPenBean>) {
                        //Logger.e(t.toString())
                        if (t.code == 0) {
                            if(t.data?.isBind == 1) {
                                penConnectState.set(true)
                                penMac = "MAC: ${t.data?.penMac}"
                            } else {
                                penConnectState.set(false)
                                penMac = "MAC: "
                            }
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showErrorToast(msg)
                    }
                })
        }

    }
}